package com.upf1sh.controller;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.upf1sh.utils.Result;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/dataswitch")
public class ApiController {

    @PostMapping("/getReboot")
    public Object getReboot(@RequestBody Object object){
        HttpRequest request = HttpUtil.createPost("https://open.iot.10086.cn/fuse/http/device/thing/property/post?topic=$sys/haNg7RykRh/XD0001/thing/property/post&protocol=http");
        request.body(JSON.toJSONString(object));
        request.header("Authorization", "version=2018-10-31&res=products%2FhaNg7RykRh%2Fdevices%2FXD0001&et=1830268800&method=md5&sign=tgjzZN4ozhPtFoKwrmbhJw%3D%3D");
        request.header("token","version=2018-10-31&res=products%2FhaNg7RykRh%2Fdevices%2FXD0001&et=1830268800&method=md5&sign=tgjzZN4ozhPtFoKwrmbhJw%3D%3D");
        HttpResponse execute = request.execute();
        return  execute;
    }



    @PostMapping("/sendApi/ApplicationsendControlRobot")
    public Object reSend(@RequestBody Map<String,Object> data){
        String post = HttpUtil.post("http://218.94.8.117:8004/dataswitch/sendApi/ApplicationsendControlRobot", JSON.toJSONString(data));
        return post;
    }
    @PostMapping("/sendApi/renjij")
    public Object reSendRenJi(@RequestBody Map<String,Object> data){
        String post = HttpUtil.post("http://ai-assistant-test.wodecorp.cn/chat/knowledge_base_chat", JSON.toJSONString(data));
        return post;
    }
    @GetMapping("/egg")
    public Object egg(HttpServletRequest req){
        HttpRequest request = HttpUtil.createGet("https://webtest08.wodecorp.cn/zhdj-admin.html#/dashboard/intelligence/questionsAndAnswersDetails/list");
        request.header("Authorization", req.getHeader("Authorization"));
        HttpResponse execute = request.execute();
        return  execute.body();
    }


    @PostMapping("/chat/knowledge_base_chat")
    public Object egg(@RequestBody Map<String,Object> data){
        String post = HttpUtil.post("https://ai-assistant-test.wodecorp.cn/chat/knowledge_base_chat",JSON.toJSONString(data));
        return post;
    }

    @PostMapping("news")
    public Result news(@RequestParam Map<String,Object> data, HttpServletRequest req) throws IOException, ServletException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
        InputStream imageInputStream = null;
        InputStream wavInputStream = null;
        try {
            // 获取 image 和 wav 的输入流
            Part imagePart = multipartRequest.getPart("image");
            Part wavPart = multipartRequest.getPart("wav");

            if (imagePart != null && imagePart.getSize() > 0) {
                imageInputStream = imagePart.getInputStream();
            }
            if (wavPart != null && wavPart.getSize() > 0) {
                wavInputStream = wavPart.getInputStream();
            }

            // 创建 HttpClient 实例
            HttpClient httpClient = HttpClients.createDefault();
            // 创建 HttpPost 请求
            HttpPost httpPost = new HttpPost("http://newvetai.world-tech.com.cn:8092/2030/");
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();

            if (imageInputStream != null) {
                builder.addBinaryBody("image", imageInputStream, ContentType.APPLICATION_OCTET_STREAM, "image.jpg");
            }
            if (wavInputStream != null) {
                builder.addBinaryBody("wav", wavInputStream, ContentType.APPLICATION_OCTET_STREAM, "audio.wav");
            }
            builder.addTextBody("disease_caption", (String) data.get("disease_caption"), ContentType.TEXT_PLAIN);
            HttpEntity multipart = builder.build();
            // 设置请求体
            httpPost.setEntity(multipart);

            // 执行请求
            org.apache.http.HttpResponse response = httpClient.execute(httpPost);
            // 获取响应实体
            HttpEntity responseEntity = response.getEntity();

            // 将响应实体转换为字符串
            String result = EntityUtils.toString(responseEntity);

            return Result.ok().data("result", result);
        } finally {
            // 关闭输入流
            if (imageInputStream != null) {
                imageInputStream.close();
            }
            if (wavInputStream != null) {
                wavInputStream.close();
            }
        }
    }
    @PostMapping("newsPig")
    public Result newsPig(@RequestParam Map<String,Object> data, HttpServletRequest req) throws IOException, ServletException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
        InputStream imageInputStream = null;
        InputStream wavInputStream = null;
        try {
            // 获取 image 和 wav 的输入流
            Part imagePart = multipartRequest.getPart("image");
            Part wavPart = multipartRequest.getPart("wav");

            if (imagePart != null && imagePart.getSize() > 0) {
                imageInputStream = imagePart.getInputStream();
            }
            if (wavPart != null && wavPart.getSize() > 0) {
                wavInputStream = wavPart.getInputStream();
            }

            // 创建 HttpClient 实例
            HttpClient httpClient = HttpClients.createDefault();
            // 创建 HttpPost 请求
            HttpPost httpPost = new HttpPost("http://newvetai.world-tech.com.cn:8092/2023/");
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();

            if (imageInputStream != null) {
                builder.addBinaryBody("image", imageInputStream, ContentType.APPLICATION_OCTET_STREAM, "image.jpg");
            }
            if (wavInputStream != null) {
                builder.addBinaryBody("wav", wavInputStream, ContentType.APPLICATION_OCTET_STREAM, "audio.wav");
            }
            builder.addTextBody("disease_caption", (String) data.get("disease_caption"), ContentType.TEXT_PLAIN);
            HttpEntity multipart = builder.build();
            // 设置请求体
            httpPost.setEntity(multipart);

            // 执行请求
            org.apache.http.HttpResponse response = httpClient.execute(httpPost);
            // 获取响应实体
            HttpEntity responseEntity = response.getEntity();

            // 将响应实体转换为字符串
            String result = EntityUtils.toString(responseEntity);

            return Result.ok().data("result", result);
        } finally {
            // 关闭输入流
            if (imageInputStream != null) {
                imageInputStream.close();
            }
            if (wavInputStream != null) {
                wavInputStream.close();
            }
        }
    }
}

